var canvasCard = require('./canvasCard.js');
const app = getApp();

var data = {
  xhx_images: './images/bg.jpeg',
  // 请求参数
  form: {
    userId: '',
    matchType: '', // 1、官方，2、自建
    id: ''
  },
};

var currentPage = {
  data,
  form: {
  }, //详情请求参数
  onLoad,
  loadInit, //跳转到登录授权成功后返回
  getDetail,
  newCanvas, //名片生成
  canvasToTempFilePath, //保存到手机
  onShareAppMessage: function() {
    const that = this;
    console.log(that.data.form.matchType)
    const uuid = that.data.uuid;
    return {
      title: '',
      path: app.xhx_route[that.data.form.matchType == 2 ? 'selfChallengeDetail' :'challenge'] + "?uuid=" + that.data.form.id
    }
  }
};

var page = Object.assign({}, currentPage);

Page(page);

function onLoad(options) {
  if (!options.id || !options.matchType) {
    wx.showToast({
      icon: 'none',
      title: '打卡赛id和打卡赛类型matchType不能为空',
      duration: 5000
    })
    setTimeout(() => {
      wx.navigateBack({})
    }, 5000)
    return
  }

  // 处理参数
  const that = this;
  const form = this.data.form
  form.matchType = options.matchType
  form.id = options.id

  that.setData({
    form
  })
  
  var _this = this
  app.middleware.requestAjax({
    serAddr: app.serAddrs.createChallenge,
    data: _this.data.form,
    cancelToast: true,
    success: function (res) {
      var { name,
        bonusPool,
        headPortrait,
        qrCodeUrl,
        dayAim,
        customScheduleDays,
        username,
        targetType } = res.data
      wx.getImageInfo({
        src: headPortrait,
        success(res) {
          headPortrait = res.path

          wx.getImageInfo({
            src: qrCodeUrl,
            success(res) {
              qrCodeUrl = res.path

              _this.setData({
                name,
                bonusPool,
                headPortrait,
                qrCodeUrl,
                dayAim,
                customScheduleDays,
                username,
                targetType
              })

              _this.newCanvas();
            }
          })  
        }
      })  
    },
    fail: function (res) {
      app.middleware.showToast(res && res.msg || "加载失败");
    }
  })
  


  app.xlogin.getUser(function(res) {
    if (res) that.loadInit();

    var myInfo = app.isNavigateToLogin();
    if (!myInfo) return;

    myInfo = app.isNavigateToBind();
    if (!myInfo) return;
  })
}

function loadInit() {
  const that = this;
  that.getDetail();
}

function getDetail() {
  const that = this;
  const activityUuid = that.data.activityUuid;
  let temp = {};

  app.middleware.requestAjax({
    serAddr: app.serAddrs[activityUuid ? "getQcForShare" : "getQCode"],
    data: that.form,
    cancelToast: true,
    success: function(res) {
      let xbody = res.data;
      xbody.headPortrait = app.userInfo.headPortrait;
      xbody.sex = app.userInfo.sex;
      xbody.username = app.userInfo.username;
      xbody.createTime = app.middleware.time().typeB;
      if (xbody.qrCodeUrl) {
        app.downloadFile.downloadFile({
          url: xbody.qrCodeUrl,
          success: function(path) {
            xbody.qrCodeUrl = path;

            if (xbody.headPortrait) {
              app.downloadFile.downloadFile({
                url: xbody.headPortrait,
                success: function(path) {
                  xbody.headPortrait = path;
                  temp.xbody = xbody;
                  that.setData(temp);
                  that.newCanvas();
                }
              })
            } else {
              temp.xbody = xbody;
              that.setData(temp);
              that.newCanvas();
            }

          }
        })
      }
    }
  })
}

/**
 * 名片生成
 */
function newCanvas() {
  var that = this;
  var xbody = that.data.xbody || {};

  if (xbody.createTime) {
    let arr = xbody.createTime.split(" ");
    xbody.createTime = arr[0];
  }
  var { name,
    bonusPool,
    headPortrait,
    qrCodeUrl,
    dayAim,
    customScheduleDays,
    username,
    targetType } = this.data
  let data = {
    name,
    bonusPool,
    headPortrait,
    qrCodeUrl,
    dayAim,
    customScheduleDays,
    username,
    targetType,
    matchType: this.data.form.matchType
  };
  console.log(this.data)

  console.log("海报卡片参数", data);

  if (that.myCanvas) {
    that.myCanvas.canvasData(data)
    return;
  }

  that.myCanvas = new canvasCard.canvas({
    canvasId: 'myCanvas',
    data,
    changeSize: function(res) {
      that.setData(res)
      console.log('changeSize', res)
    },
    success: function() {
      that.setData({
        msg: ''
      })
    }
  });
}

/**
 * 保存到手机
 */
function canvasToTempFilePath() {
  var myInfo = app.isNavigateToLogin();
  if (!myInfo) return;

  myInfo = app.isNavigateToBind();
  if (!myInfo) return;

  if (this.myCanvas) this.myCanvas.canvasToTempFilePath();
}